package io.renren.utils;

import io.renren.config.BusinessException;
import io.renren.config.ResponseCodeEnum;
import lombok.Data;

import java.io.Serializable;

@Data
public class CommonResponse<T> implements Serializable {
    private static final long serialVersionUID = 3978461145132490879L;

    private String code;// 返回码

    private String msg;// 返回消息

    private T data;// 数据源

    public CommonResponse(String code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public CommonResponse(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public CommonResponse(ResponseCodeEnum responseCodeEnum) {
        this.code = responseCodeEnum.getCode();
        this.msg = responseCodeEnum.getMessage();
    }

    public CommonResponse(BusinessException exception) {
        this.code = exception.getCode();
        this.msg = exception.getMessage();
    }

    public static <T> CommonResponse success(T data) {
        return new CommonResponse<>(ResponseCodeEnum.SUCCESS.getCode(),
                ResponseCodeEnum.SUCCESS.getMessage(), data);
    }

    public static <T> CommonResponse success(T data, String msg) {
        return new CommonResponse<>(ResponseCodeEnum.SUCCESS.getCode(), msg, data);
    }

    public static CommonResponse success() {
        return success(null);
    }

    public static <T> CommonResponse error(String msg) {
        return new CommonResponse<>(ResponseCodeEnum.FAIL.getCode(), msg, null);
    }

    public static <T> CommonResponse error(String code, String msg) {
        return new CommonResponse<>(code, msg, null);
    }

    public static <T> CommonResponse error(String code, String msg, T data) {
        return new CommonResponse<>(code, msg, data);
    }
}